package mdl

import (
	"github.com/axgle/mahonia"
	"github.com/opesun/goquery"
	"hotime"
	"io/ioutil"
	"net/http"
)

func GetYazhi(fid string) []hotime.Map {
	ou:=OuZhi{}
	yu:=Yuche{}
	ouzhi:=ou.Info(fid)
	db := hotime.Db

	data := make([]hotime.Map, 0)
	get := db.Get("ball", "*", hotime.Map{"AND": hotime.Map{"fid": fid, "type": "0"}})
	if get != nil {
		data = db.Select("yazhi", "*", hotime.Map{"AND": hotime.Map{"fid": fid, "type": "0"}})
		if data != nil && len(data) != 0 {

			return data
		}
	}

	var url = "http://odds.500.com/fenxi/yazhi-" + fid + ".shtml"
	client := http.Client{}
	res, _ := client.Get(url)
	defer res.Body.Close()
	body, err := ioutil.ReadAll(res.Body)
	p, err := goquery.ParseString(mahonia.NewDecoder("gbk").ConvertString(string(body)))
	if err != nil {
		panic(err)
		return nil
	}
	//db, _ := sql.Open("sqlite3", "data.db")
	//defer db.Close()
	db.Delete("yazhi", hotime.Map{"AND": hotime.Map{"fid": fid, "type": "0"}})

	trs := p.Find("#datatb")
	trs = trs.Find(".tr1")

	for i := 0; i < trs.Length(); i++ {
		tds := trs.Eq(i)
		tables := tds.Find(".pl_table_data")
		tds = tds.Find("td")
		//		fmt.Println(trs.Eq(i).Html())
		yazhi := hotime.Map{"fid": fid, "type": 0}
		a := tds.Eq(1)
		a = a.Find("a")
		yazhi["quanchenghref"] = a.Attr("href")
		yazhi["quancheng"] = a.Attr("title")
		table0 := tables.Eq(0)
		ttds := table0.Find("td")
		yazhi["ping"] = ttds.Eq(0).Text()
		yazhi["pan"] = ttds.Eq(1).Text()
		yazhi["ying"] = ttds.Eq(2).Text()
		yazhi["time"] = tds.Eq(7).Text()

		table1 := tables.Eq(1)
		ttds = table1.Find("td")
		yazhi["cping"] = ttds.Eq(0).Text()
		yazhi["cpan"] = ttds.Eq(1).Text()
		yazhi["cying"] = ttds.Eq(2).Text()
		//		fmt.Println(tds.Eq(12).Html())
		yazhi["ctime"] = tds.Eq(12).Text()
		a = tds.Eq(13)
		a = a.Find("a")
		yazhi["masterhref"] = a.Eq(0).Attr("href")
		yazhi["slavehref"] = a.Eq(1).Attr("href")
		yazhi["synchref"] = a.Eq(2).Attr("href")
		//sql := "insert  into `yazhi`(`fid`,`quancheng`,`quanchenghref`,`ping`,`pan`,`ying`,`time`,`cping`,`cpan`,`cying`,`ctime`,`masterhref`,`slavehref`,`synchref`) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?);"
		//db.Exec(sql, yazhi.Fid, yazhi.Quancheng, yazhi.QuanchengHref, yazhi.Ping,
		//	yazhi.Pan, yazhi.Ying, yazhi.Time, yazhi.Cping, yazhi.Cpan, yazhi.Cying, yazhi.Ctime, yazhi.Masterhref, yazhi.Slavehref, yazhi.Synchref)
		if ouzhi[yazhi.GetString("quancheng")]!=nil {
			tempOuzhi:=ouzhi[yazhi.GetString("quancheng")]
			ouMax:=float64(0)
			if ouMax<tempOuzhi.GetFloat64("sheng"){
				ouMax=tempOuzhi.GetFloat64("sheng")
			}
			if ouMax<tempOuzhi.GetFloat64("ping"){
				ouMax=tempOuzhi.GetFloat64("ping")
			}
			if ouMax<tempOuzhi.GetFloat64("fu"){
				ouMax=tempOuzhi.GetFloat64("fu")
			}
			ouZhi:=ouMax/(float64(100)-ouMax)
			fmt.Println(ouZhi)
			ouYu:=db.Get("ps","*",hotime.Map{"AND":hotime.Map{"pmax[>]":ouZhi,"pmin[<]":ouZhi}})

			if ouYu!=nil{
				//判断盘口关系
				shi:=yu.ReplacePing(yu.ReplacePan(yazhi.GetString("pan")))
				if shi>=ouYu.GetFloat64("plastmin")&&shi<=ouYu.GetFloat64("plastmax"){
					fmt.Println(ShuiWei["等"])
				}
				if shi<ouYu.GetFloat64("plastmin"){
					fmt.Println(ShuiWei["小"])
				}
				if shi>ouYu.GetFloat64("plastmax"){
					fmt.Println(ShuiWei["大"])
				}


			}
		}
		yazhi=(&Yuche{}).Get(yazhi)
			yazhi["yid"] = db.Insert("yazhi", yazhi)
		data = append(data, yazhi)
	}

	trs = p.Find("#datatb")
	trs = trs.Find(".tr2")
	for i := 0; i < trs.Length(); i++ {
		tds := trs.Eq(i)
		tables := tds.Find(".pl_table_data")
		tds = tds.Find("td")
		//		fmt.Println(trs.Eq(i).Html())
		yazhi := hotime.Map{"fid": fid, "type": 0}

		//yazhi["quancheng"] = tds.Eq(1).Text()
		a := tds.Eq(1)
		a = a.Find("a")
		yazhi["quanchenghref"] = a.Attr("href")
		yazhi["quancheng"] = a.Attr("title")
		table0 := tables.Eq(0)
		ttds := table0.Find("td")
		yazhi["ping"] = ttds.Eq(0).Text()
		yazhi["pan"] = ttds.Eq(1).Text()
		yazhi["ying"] = ttds.Eq(2).Text()
		yazhi["time"] = tds.Eq(7).Text()

		table1 := tables.Eq(1)
		ttds = table1.Find("td")
		yazhi["cping"] = ttds.Eq(0).Text()
		yazhi["cpan"] = ttds.Eq(1).Text()
		yazhi["cying"] = ttds.Eq(2).Text()
		//		fmt.Println(tds.Eq(12).Html())
		yazhi["ctime"] = tds.Eq(12).Text()
		a = tds.Eq(13)
		a = a.Find("a")
		yazhi["masterhref"] = a.Eq(0).Attr("href")
		yazhi["slavehref"] = a.Eq(1).Attr("href")
		yazhi["synchref"] = a.Eq(2).Attr("href")
		//sql := "insert  into `yazhi`(`fid`,`quancheng`,`quanchenghref`,`ping`,`pan`,`ying`,`time`,`cping`,`cpan`,`cying`,`ctime`,`masterhref`,`slavehref`,`synchref`) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?);"
		//db.Exec(sql, yazhi.Fid, yazhi.Quancheng, yazhi.QuanchengHref, yazhi.Ping,
		//	yazhi.Pan, yazhi.Ying, yazhi.Time, yazhi.Cping, yazhi.Cpan, yazhi.Cying, yazhi.Ctime, yazhi.Masterhref, yazhi.Slavehref, yazhi.Synchref)
		yazhi=(&Yuche{}).Get(yazhi)
			yazhi["yid"] = db.Insert("yazhi", yazhi)
		data = append(data, yazhi)
	}

	if len(data) == 0 {
		return nil
	}
	return data
}
